# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=blis
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.1
pkgrel=2
pkgdesc="BLAS-like Library Instantiation Software Framework. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/flame/blis'
license=('spdx:BSD-3-Clause')
conflicts=("${MINGW_PACKAGE_PREFIX}-cblas")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/flame/blis/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-disable-armsve.patch")
sha256sums=('847c035809b8994c077ade737a4813601db96c4cf0d903d08ba6a9b8ee0fe03e'
            'd77cbcf26d7abae0af7dd8b1ffa433f2717e458fc64edb4fc67968f8735245a2')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 --binary -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    001-disable-armsve.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}
  rm -f Makefile blis.pc.in common.mk
  rm -rf config

  if [[ ${CARCH} == x86_64 ]]; then
    _confname=amd64
  else
    _confname=arm64
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _fc=flang
  else
    _fc=gfortran
  fi

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --enable-cblas \
    --enable-threading=openmp,pthreads \
    --disable-static \
    FC=${_fc} \
    LDFLAGS="${LDFLAGS} -lpthread" \
    "${_confname}"

  make libs
}

check() {
  cd build-${MSYSTEM}

  if [[ ${CARCH} == x86_64 ]]; then
    _confname=amd64
  else
    _confname=arm64
  fi

  PATH="${srcdir}"/build-${MSYSTEM}/lib/${_confname}:$PATH \
  make check || true
}

package() {
  cd build-${MSYSTEM}

  make DESTDIR="${pkgdir}" install

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/bin
  mv "${pkgdir}"${MINGW_PREFIX}/lib/*.dll "${pkgdir}"${MINGW_PREFIX}/bin/

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
